package com.hualan.service.service.service;

import com.hualan.mapper.UserMapper;
import com.hualan.pojo.User;
import com.hualan.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    public Integer login(String username, String password) {
        return userMapper.login(username, password);
    }

    public void register(User user) {
        userMapper.register(user);
    }

    public String findNickname(Integer uid) {
        return userMapper.findNickname(uid);
    }

    public String findPhone(Integer uid) {
        return userMapper.findPhone(uid);
    }

    public Boolean updatePwd(Integer id, String oldPwd, String newPwd) {
        String nickname = userMapper.findNickname(id);
        if(MD5Util.encode(oldPwd + nickname).equals(userMapper.findPwd(id))) {

            if(userMapper.updatePhoneOrPwd(id,null,MD5Util.encode(newPwd + nickname)) == 1) {
                return true;
            } else {
                System.out.println("返回影响行不为一，更新密码失败");
                return false;
            }

        } else {
            return false;
        }
    }

    public Boolean updatePhone(Integer id, String phone) {
        if(userMapper.updatePhoneOrPwd(id,phone,null) == 1) {
            return true;
        } else {
            System.out.println("返回影响行不为一，更新手机号失败");
            return false;
        }
    }
}
